var logger = require('../../common').logger;
var query = require('../../common').Query;
var map = require('../../common').Mapping;

exports.findPlanExtra = function (req, res) {
    query(map.sql.findPlanExtra, res, req.body, function (rows) {
        res.send(rows);
    });
};

exports.addPlanExtra = function (req, res) {
    query(map.sql.deleteAllCurrentExtra, res, {pm_id: req.session.uid,plan_id : parseInt(req.cookies.latest)}, function () {
        var count = 0;
        if(req.body.length == 0) {
            res.send("success");
            return;
        }
        req.body.forEach(function (e) {
            query(map.sql.addPlanExtra, res, e, function (rows) {
                if (rows.affectedRows != 0) {
                    count++;
                    if (count == req.body.length) {
                        res.send("success")
                    }
                } else {
                    res.send({error: 503, description: map.error[503]});
                    return;
                }
            });
        });
    });
};